/*
"JSFXGEN" - Debug.as
Modular Integrated Development Environment for JesuSonic
Project page: http://jsfxgen.googlecode.com

Copyright 2009 Lubomir I. Ivanov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package neolit123.ui
{
  // =========================================================
  // [ import
  // =========================================================
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFormat;
  import flash.events.MouseEvent;
  // =========================================================
  // [ class
  // =========================================================
  public class Debug extends Sprite
  {
    private const ddimw:int = 300;
    private const ddimh:int = 150;
    private var mc:Sprite = new Sprite;
    private var sb:UIScrollBar;
    private var dstring:String = "";
    private var tf:TextFormat = new TextFormat("_sans", 10);
    private var txt:TextField = new TextField;
    // =========================================================
    // [ create debug
    // =========================================================
    public function Debug():void
    {  
      // @ add main mc
      addChild(mc);
      // @ add txt
      mc.addChild(txt);
      with (txt)
      {
        html = true;
        tf.indent = 3;
        defaultTextFormat = tf;
        border = true;
        background = true; 
        backgroundColor = 0xfafafa;
        x = -ddimw;
        y = -ddimh;
        wordWrap = true;
        height = ddimh;
        width = ddimw;
      }      
      // @ add scroll
      sb = new UIScrollBar(txt,15);
      mc.addChild(sb);
      // @ add clear btn
      var cls:TextButton = new TextButton("C",0xffffff,0x0072ba,0xffffff);
      mc.addChild(cls);
      cls.x = -15;
      Main.tt.add(cls,"Clear History");
      cls.addEventListener(MouseEvent.MOUSE_DOWN, clearf);
      function clearf(e:MouseEvent = null):void
      {
        dstring = txt.text = "";
        df("History cleared");
      }
      // @ add hide/showbtn
      var btn:TextButton = new TextButton("D",0xffffff,0x000000,0xffffff);
      addChild(btn);
      Main.tt.add(btn,"Debugger");
      btn.addEventListener(MouseEvent.MOUSE_DOWN, debugvis);
      function debugvis(e:MouseEvent = null):void {
        mc.visible = !mc.visible;
      }
      // @ hide
      mc.visible = false;
    }
    // =========================================================
    // [ debug text
    // =========================================================
    public function df(str:*,...rest:Array):void
    {
      // @ url encode
      str = str.toString();
      str = str.split("\'").join("&apos;");
      str = str.split("\"").join("&quot;");
      str = str.split("&").join("&amp;");
      str = str.split("<").join("&lt;");
      str = str.split(">").join("&gt;");
      // if (!visible) visible = true; // ?
      var tmpstring:String;
      var dt:Date = new Date();
      tmpstring = dt.toLocaleTimeString()+" ";
      // @ is error ?
      if (Boolean(rest[0])) {
        tmpstring += "<font color='#960202'>### "+str+"</font>";
        mc.visible = true;
      } else tmpstring += "--- "+str;
      dstring += tmpstring+"\n";
      txt.htmlText = dstring;  
      txt.setTextFormat(tf);
      txt.scrollV = txt.maxScrollV;
    }
  }
}